package com.bit.catmoney;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bit.catmoney.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;


public class RankActivity extends Activity {
	private ArrayList<RankData> arData;
	private RankAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rank);

		arData = new ArrayList<RankData>();
		adapter = new RankAdapter(this, R.layout.rank_list_item, arData);

		ListView lv = (ListView) this.findViewById(R.id.rank_list);
		lv.setAdapter(adapter);
		
		GameServer server = new GameServer();
		server.receiveRankTop(mAfterDown);
		
//		server.sendScore(nickname, score, null);
	}

	Handler mAfterDown = new Handler() {
		public void handleMessage(Message msg) {
			String json = (String) msg.obj;
			try {
				JSONArray ja = new JSONArray(json);
				for (int i = 0; i < ja.length(); i++) {
					JSONObject jo = ja.getJSONObject(i);
					String n = jo.getString("nickname");
					int s = jo.getInt("score");
					
					arData.add(new RankData(n, s));
					
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			adapter.notifyDataSetChanged();
		}
	};
}
